package com.clear.honghefinance.admin.controller;

import com.clear.honghefinance.biz.modules.system.member.domain.dto.GetUserSmsCodeDTO;
import com.clear.honghefinance.biz.modules.system.member.domain.dto.UpdateEmailAndNameDTO;
import com.clear.honghefinance.biz.modules.system.member.domain.dto.UpdatePasswordDTO;
import com.clear.honghefinance.biz.modules.system.member.domain.dto.UpdatePhoneDTO;
import com.clear.honghefinance.biz.modules.system.member.domain.vo.AdminDTO;
import com.clear.honghefinance.biz.modules.system.member.domain.vo.CurrentInfoVO;
import com.clear.honghefinance.biz.modules.system.member.service.IMemberService;
import com.clear.honghefinance.biz.modules.system.member.service.MemberComService;
import com.clear.honghefinance.biz.modules.system.member.service.MemberBindPhoneService;
import com.clear.honghefinance.common.dto.entity.ApiResponse;
import com.clear.honghefinance.common.service.TokenService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@Api(tags = "用户模块")
@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {

    @Resource
    private MemberComService memberComService;
    @Resource
    private IMemberService memberService;
    @Resource
    private MemberBindPhoneService memberBindPhoneService;
    @Resource
    TokenService<AdminDTO> tokenService;

    /**
     * 获取当前登录用户信息
     *
     * @return
     */
    @ApiModelProperty("获取当前登录用户信息")
    @GetMapping("/currentUser")
    public ApiResponse<CurrentInfoVO> currentUser() {
        return ApiResponse.success(memberComService.getCurrentInfo());
    }

    /**
     * 退出登录
     *
     * @return
     */
    @ApiOperation(value = "退出登录")
    @PostMapping(value = "/loginOut")
    public ApiResponse<Void> loginOut() {
        tokenService.clearToken();
        return ApiResponse.success();
    }

    /**
     * 修改邮箱和姓名
     *
     * @param dto
     * @return
     */
    @ApiOperation(value = "修改邮箱和姓名")
    @PostMapping(value = "/updateEmailAndName")
    public ApiResponse<Boolean> updateEmailAndName(@Validated @RequestBody UpdateEmailAndNameDTO dto) {
        return ApiResponse.success(memberService.updateEmailAndName(dto));
    }

    /**
     * 修改密码、修改租户密码
     *
     * @param dto
     * @return
     */
    @ApiOperation(value = "修改密码")
    @PostMapping(value = "/updatePassword")
    public ApiResponse<Boolean> updatePassword(@Validated @RequestBody UpdatePasswordDTO dto) {
        return ApiResponse.success(memberBindPhoneService.updatePassword(dto));
    }

    /**
     * 获取图形验证码（用于修改手机号）
     * @return base64图形验证码
     */
    @ApiOperation(value = "获取图形验证码")
    @GetMapping(value = "/getBase64Code")
    public ApiResponse<String> getBase64Code() {
        String result = memberComService.getBase64Code();
        return ApiResponse.success(result);
    }

    /**
     * 获取短信验证码（用于修改手机号）
     * @param dto
     * @return
     */
    @ApiOperation(value = "获取短信验证码")
    @GetMapping(value = "/sendSmsCode")
    public ApiResponse<Void> sendSmsCode(@Validated @ModelAttribute GetUserSmsCodeDTO dto) {
        memberComService.sendSmsCode(dto);
        return ApiResponse.success();
    }

    /**
     * 修改手机号
     * @param dto
     * @return
     */
    @ApiOperation(value = "修改手机号")
    @PostMapping(value = "/updatePhone")
    public ApiResponse<Boolean> updatePhone(@Validated @RequestBody UpdatePhoneDTO dto) {
        return ApiResponse.success(memberComService.updatePhone(dto));
    }


}
